export function flattenRoutes(routes: Route[]): Route[] {
  let result: Route[] = [];
  for (let i = 0; i < routes.length; i++) {
    const route = routes[i];
    // 判断是否需要授权验证，需要的话直接跳过这个路由
    if (route.auth) {
      continue;
    }
    // 将当前路由加入结果数组中
    result.push(route);
    // 如果当前路由有子路由，递归遍历子路由
    if (route.children) {
      result = result.concat(flattenRoutes(route.children));
    }
  }
  return result;
}
// 根据路由表与路径获取面包屑
export function getBreadcrumbs(
  routes: Route[],
  path: string,
): { title: string }[] {
  const items = [];
  const segments = path.split('/').filter(Boolean);

  let currentPath = '';
  for (let i = 0; i < segments.length; i++) {
    currentPath += '/' + segments[i];
    const route = findRouteByPath(routes, currentPath);
    if (route) {
      items.push({ title: route.name, icon: route.icon });
      if (route.children) {
        const childItems = getBreadcrumbs(route.children, currentPath);
        items.push(...childItems);
      }
    }
  }
  return items;
}

export function findRouteByPath(routes: Route[], path: string): Route | null {
  for (const route of routes) {
    if (route.path === path) {
      return route;
    }
    if (route.children) {
      const childRoute = findRouteByPath(route.children, path);
      if (childRoute) {
        return childRoute;
      }
    }
  }
  return null;
}
